// Import required libraries
import axios from 'axios';
import cheerio from 'cheerio';

// Function to fetch the HTML of a webpage
async function fetchHTML(url: string): Promise<string> {
  try {
    const { data } = await axios.get(url);
    return data;
  } catch (error) {
    console.error(`Error fetching HTML: ${error}`);
    throw error;
  }
}

// Function to extract and parse data from HTML using Cheerio
function extractDataFromHTML(html: string): void {
  const $ = cheerio.load(html);

  // Example: Extracting all the links (anchor tags) from the webpage
  $('a').each((index, element) => {
    const link = $(element).attr('href');
    const text = $(element).text().trim();
    console.log(`Link: ${link}, Text: ${text}`);
  });
}

// Common utility function to clean data
function cleanText(text: string): string {
  return text.replace(/\s+/g, ' ').trim();
}

// Function to start the scraping process
async function startScraping(url: string): Promise<void> {
  try {
    const html = await fetchHTML(url);
    extractDataFromHTML(html);
  } catch (error) {
    console.error(`Error during scraping: ${error}`);
  }
}

// Example URL for scraping
const url = 'https://example.com';

// Start scraping
startScraping(url);
